﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Lab7_MutilLanguage
{
    public class DataProvider
    {
        private string _myConnectionString;
        private SqlConnection connection;
        private SqlDataAdapter adapter;
        private SqlCommand command;

        public string MyConnectionString
        {
            get { return _myConnectionString; }
            set { _myConnectionString = value; }
        }

        public void connect()
        {
            //connection = new SqlConnection(MyConnectionString);
            connection = new SqlConnection(MyConnectionString);
            connection.Open();
        }

        public void disconnect()
        {
            connection.Close();
        }

        public void executeNonQuery(string sqlString)
        {
            command = new SqlCommand(sqlString, connection);
            command.ExecuteNonQuery();
        }

        public object executeScalar(string sqlString)
        {
            command = new SqlCommand(sqlString, connection);
            return command.ExecuteScalar();
        }

        public DataTable getDepartments()
        {
            DataTable dt = new DataTable();
            try
            {
                connect();
                string sql = "Select * From Departments";
                adapter = new SqlDataAdapter(sql, connection);
                adapter.Fill(dt);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        public DataTable getContent()
        {
            DataTable dt = new DataTable();
            try
            {
                connect();
                string sql = "Select * From Content";
                adapter = new SqlDataAdapter(sql, connection);
                adapter.Fill(dt);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        public DataTable getLanguage()
        {
            DataTable dt = new DataTable();
            try
            {
                connect();
                string sql = "Select * From Language";
                adapter = new SqlDataAdapter(sql, connection);
                adapter.Fill(dt);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }
    }
}